<template>
  <div>
    <zh-card title="">
      <zh-form :model="pageData.queryParams" ref="queryFormRef">
        <zh-form-select prop="datasourceType" label="数据源类型" allowClear :span="8"
                        :options="pageData.dictData.DATASOURCETYPE"
                        v-model="pageData.queryParams.datasourceType"></zh-form-select>
        <zh-form-input prop="fieldTypeName" label="字段类型名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.fieldTypeName"></zh-form-input>
        <zh-form-select prop="optionName" label="可填字段" allowClear :span="8" :options="pageData.dictData.OPTIONNAME"
                        v-model="pageData.queryParams.optionName"></zh-form-select>
        <zh-form-select prop="optionType" label="可填字段类型" allowClear :span="8" :options="pageData.dictData.OPTIONTYPE"
                        v-model="pageData.queryParams.optionType"></zh-form-select>
        <zh-form-input prop="optionMinVal" label="最小值" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.optionMinVal"></zh-form-input>
        <zh-form-input prop="optionMaxVal" label="最大值" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.optionMaxVal"></zh-form-input>
        <zh-form-input prop="optionDefaultVal" label="默认值" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.optionDefaultVal"></zh-form-input>
        <zh-form-select prop="optionNullable" label="是否可为空" allowClear :span="8"
                        :options="pageData.dictData.OPTIONNULLABLE"
                        v-model="pageData.queryParams.optionNullable"></zh-form-select>
        <zh-form-query @queryClick="queryFieldTypeMeta" :formRef="queryFormRef" :show-advanced="false">
          <el-button @click="showAddDialog">添加</el-button>
        </zh-form-query>
      </zh-form>
    </zh-card>
    <zh-table title="字段类型列表" ref="fieldTypeMetaTable"
              size="mini"
              :funcCode="'fieldTypeMeta0001'"
              :columns="pageData.tableColumns"
              highlight-current-row>
      <template #op="scope">
        <el-tooltip content="编辑" placement="top">
          <el-button :icon="Edit" link @click="showEditDialog(scope)"></el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button :icon="Delete" type="danger" link @click="delFieldTypeMeta(scope.row)"></el-button>
        </el-tooltip>
        <el-tooltip content="详情" placement="top">
          <el-button :icon="Document" link @click="showDetail(scope)"></el-button>
        </el-tooltip>
      </template>
    </zh-table>
    <el-dialog title="添加字段类型" v-model="pageData.addFieldTypeMetaDialogVisible">
      <zh-form :rules="pageData.formRules">
        <zh-form-select prop="datasourceType" label="数据源类型" allowClear :span="8"
                        :options="pageData.dictData.DATASOURCETYPE"
                        v-model="pageData.formData.datasourceType"></zh-form-select>
        <zh-form-input prop="fieldTypeName" label="字段类型名称" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.fieldTypeName"></zh-form-input>
        <zh-form-select prop="optionName" label="可填字段" allowClear :span="8" :options="pageData.dictData.OPTIONNAME"
                        v-model="pageData.formData.optionName"></zh-form-select>
        <zh-form-select prop="optionType" label="可填字段类型" allowClear :span="8" :options="pageData.dictData.OPTIONTYPE"
                        v-model="pageData.formData.optionType"></zh-form-select>
        <zh-form-input prop="optionMinVal" label="最小值" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.optionMinVal"></zh-form-input>
        <zh-form-input prop="optionMaxVal" label="最大值" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.optionMaxVal"></zh-form-input>
        <zh-form-input prop="optionDefaultVal" label="默认值" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.optionDefaultVal"></zh-form-input>
        <zh-form-select prop="optionNullable" label="是否可为空" allowClear :span="8"
                        :options="pageData.dictData.OPTIONNULLABLE"
                        v-model="pageData.formData.optionNullable"></zh-form-select>
      </zh-form>
      <template #footer>
        <span class="dialog-footer">

          <el-button @click="pageData.addFieldTypeMetaDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addFieldTypeMeta">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog title="修改字段类型" v-model="pageData.editFieldTypeMetaDialogVisible">
      <zh-form :rules="pageData.formRules">
        <zh-form-select prop="datasourceType" label="数据源类型" allowClear :options="pageData.dictData.DATASOURCETYPE"
                        v-model="pageData.editFormData.datasourceType"></zh-form-select>
        <zh-form-input prop="fieldTypeName" label="字段类型名称"
                       v-model="pageData.editFormData.fieldTypeName"></zh-form-input>
        <zh-form-select prop="optionName" label="可填字段" allowClear :options="pageData.dictData.OPTIONNAME"
                        v-model="pageData.editFormData.optionName"></zh-form-select>
        <zh-form-select prop="optionType" label="可填字段类型" allowClear :options="pageData.dictData.OPTIONTYPE"
                        v-model="pageData.editFormData.optionType"></zh-form-select>
        <zh-form-input prop="optionMinVal" label="最小值"
                       v-model="pageData.editFormData.optionMinVal"></zh-form-input>
        <zh-form-input prop="optionMaxVal" label="最大值"
                       v-model="pageData.editFormData.optionMaxVal"></zh-form-input>
        <zh-form-input prop="optionDefaultVal" label="默认值"
                       v-model="pageData.editFormData.optionDefaultVal"></zh-form-input>
        <zh-form-select prop="optionNullable" label="是否可为空" allowClear :options="pageData.dictData.OPTIONNULLABLE"
                        v-model="pageData.editFormData.optionNullable"></zh-form-select>
      </zh-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="pageData.editFieldTypeMetaDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editFieldTypeMeta">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup name="fieldTypeMeta">
import {Delete, Edit, Document} from '@element-plus/icons-vue'
import {ElMessageBox} from "element-plus";
// ------------------接收传入参数(组件)-------------------

// ------------------钩子函数----------------------------
// 初始化
onMounted(() => {
  init();
})

// ------------------定义子组件--------------------------
const fieldTypeMetaTable = ref();
const queryFormRef = ref();
// ------------------变量区------------------------------
const pageData = reactive({
  addFieldTypeMetaDialogVisible: false,
  editFieldTypeMetaDialogVisible: false,
  detailDialog: false,
  tableColumns: [
    {type: 'seq', width: 60, fixed: 'left', align: 'center', title: '序号'},
    {field: 'fieldTypeId', title: '字段类型主键', visible: false, align: 'center', width: 100},
    {field: 'datasourceType', title: '数据源类型', visible: false, align: 'center', width: 100},
    {field: 'datasourceTypeName', title: '数据源类型', visible: true, align: 'center', width: 100},
    {field: 'fieldTypeName', title: '字段类型名称', visible: true, align: 'center', width: 100},
    {field: 'optionName', title: '可填字段', visible: false, align: 'center', width: 100},
    {field: 'optionNameName', title: '可填字段中文', visible: true, align: 'center', width: 100},
    {field: 'optionRemark', title: '可填字段说明', visible: false, align: 'center', width: 100},
    {field: 'optionType', title: '可填字段类型', visible: false, align: 'center', width: 100},
    {field: 'optionTypeName', title: '可填字段类型', visible: true, align: 'center', width: 100},
    {field: 'optionMinVal', title: '最小值', visible: true, align: 'center', width: 100},
    {field: 'optionMaxVal', title: '最大值', visible: true, align: 'center', width: 100},
    {field: 'optionDefaultVal', title: '默认值', visible: true, align: 'center', width: 100},
    {field: 'optionNullable', title: '是否可为空', visible: false, align: 'center', width: 100},
    {field: 'optionNullableName', title: '是否可为空', visible: true, align: 'center', width: 100},
    {field: 'op', title: '操作', visible: true, align: 'center', width: 150, fixed: 'right', slots: {default: 'op'}},
  ],
  queryParams: {
    datasourceType: '',
    fieldTypeName: '',
    optionName: '',
    optionType: '',
    optionMinVal: '',
    optionMaxVal: '',
    optionDefaultVal: '',
    optionNullable: ''
  },
  formData: {
    fieldTypeId: '',
    datasourceType: '',
    fieldTypeName: '',
    optionName: '',
    optionRemark: '',
    optionType: '',
    optionMinVal: '',
    optionMaxVal: '',
    optionDefaultVal: '',
    optionNullable: ''
  },
  editFormData: {
    fieldTypeId: '',
    datasourceType: '',
    fieldTypeName: '',
    optionName: '',
    optionRemark: '',
    optionType: '',
    optionMinVal: '',
    optionMaxVal: '',
    optionDefaultVal: '',
    optionNullable: ''
  },
  formRules: {},
  dictFields: 'DATASOURCETYPE,OPTIONNAME,OPTIONTYPE,OPTIONNULLABLE',
  dictData: [],
});
// ------------------方法区------------------------------
const init = () => {
  getDict();
  queryFieldTypeMeta();
}
// 查询数据字典信息
const getDict = () => {
  let params = {
    dictFields: pageData.dictFields,
  };
  $api.common.getBatchDict.post(params).then((res: any) => {
    pageData.dictData = res.result;
  });
};
const queryFieldTypeMeta = () => {
  fieldTypeMetaTable.value.query(pageData.queryParams);
}
const showAddDialog = () => {
  pageData.addFieldTypeMetaDialogVisible = true;
  pageData.formData = {
    fieldTypeId: '',
    datasourceType: '',
    fieldTypeName: '',
    optionName: '',
    optionRemark: '',
    optionType: '',
    optionMinVal: '',
    optionMaxVal: '',
    optionDefaultVal: '',
    optionNullable: ''
  }
}
const showEditDialog = (scope) => {
  pageData.editFieldTypeMetaDialogVisible = true;
  pageData.editFormData = {...scope.row}
}
const addFieldTypeMeta = () => {
  $api.common.send.post('fieldTypeMeta0004', pageData.formData).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('添加字段类型成功');
      pageData.addFieldTypeMetaDialogVisible = false;
      queryFieldTypeMeta();
    }
  })
}
const editFieldTypeMeta = () => {
  $api.common.send.post('fieldTypeMeta0005', pageData.editFormData).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('修改字段类型成功');
      pageData.editFieldTypeMetaDialogVisible = false;
      queryFieldTypeMeta();
    }
  })
}
const delFieldTypeMeta = ({fieldTypeId}) => {
  ElMessageBox.confirm('确定删除该字段类型?').then(() => {
    $api.common.send.post('fieldTypeMeta0006', {fieldTypeId: fieldTypeId}).then(res => {
      if (res.code == 0) {
        ElMessageBox.alert('删除字段类型成功');
        queryFieldTypeMeta();
      }
    })
  })
}

</script>
<style lang="scss" scoped></style>
